package com.agh.portal.web;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.agh.portal.domain.Ad;
import com.agh.portal.domain.AghGroup;
import com.agh.portal.domain.GroupEvent;
import com.agh.portal.domain.MainPagePost;
import com.agh.portal.domain.Question;
import com.agh.portal.domain.Student;
import com.agh.portal.service.FileService;
import com.agh.portal.service.SecurityUtils;
import com.agh.portal.service.StudentService;

@RequestMapping("/dashboard")
@Controller
public class DashboardController {
	
	@Autowired
	StudentService studentService;
	
	@Autowired
	FileService fileService;
	
	@Autowired
	SecurityUtils securityUtils;

	@RequestMapping(produces = "text/html")
	public String show(Model uiModel){
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName(); //get logged in username
	    Student student = Student.findStudentsByEmailAddress(name).getSingleResult();
		if(student==null){
			return "redirect:/";
		}
		
		Set<AghGroup> aghGroups = student.getAghGroups();
		List<GroupEvent> groupEvents = new ArrayList<GroupEvent>();
		for(AghGroup group : aghGroups){
			groupEvents.addAll(GroupEvent.findLatestGroupEventsByAghGroup(group, 10).getResultList());
		}
		uiModel.addAttribute("events", groupEvents);
        uiModel.addAttribute("aghGroups", aghGroups);
        uiModel.addAttribute("courses", studentService.getCoursesByStudent(student));
        uiModel.addAttribute("ads", Ad.findLatestAdsByStudent(student, 4).getResultList());
        uiModel.addAttribute("mainPagePosts",  MainPagePost.findLatestMainPagePostsByStudent(student, 4).getResultList());
        uiModel.addAttribute("questions", Question.findLatestQuestionsByStudent(student, 4).getResultList());
        uiModel.addAttribute("student", student);
		
		return "dashboard";
	}
	
	@RequestMapping(value="/uploadPicture", method = RequestMethod.POST, produces = "text/html")
    public String uploadPicture(@RequestParam MultipartFile file, @RequestParam Long studentId) {	
		Student student = Student.findStudent(studentId);
		if(!securityUtils.isLoggedUserAndStudentMatch(student)) return "/";
		if (!file.isEmpty() && student != null) {
			try {
				String savedFileName = fileService.save(file, "images" + File.separator + "user" + File.separator);
				student.setAvatarId(savedFileName);
				student.persist();
				securityUtils.refreshAuthenticatedUserPrincipal();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return "redirect:/dashboard"; 
    }
	
}
